///\todo fix documentation
//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2011 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_IO_LINES_H_
#define _BUOLA_IO_LINES_H_

#include <buola/iterator/facade.h>
#include <buola/iterator/range.h>

namespace buola { namespace io {

///\addtogroup io
///@{

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///
/// iterator filter adaptor
///
/// Adapts another iterator (the underlying iterator) so that elements that don't match a certain predicate are
/// skipped when iterating.
///
/// So that it can safely skip elements and know where to stop, it must hold a copy of the end iterator. For this
/// reason, a single iterator can't be filtered, and no filter_iterator function is provided
///
/// \tparam tSub the underlying iterator type
/// \tparam tPred the type of the predicate function or function object. It must take as argument the type returned by
///         dereferencing the underlying operator and return a type convertible to boolean
///
/// For an example, see the helper function \ref filter_range
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

class ILines : public IFacade<ILines,std::input_iterator_tag,const std::string>
{
    typedef IFacade<ILines,std::input_iterator_tag,const std::string> TFacade;
    friend TFacade;

public:
    ///default constructor (invalid iterator)
    ILines()
        :   mStream(nullptr)
        ,   mOk(false)
    {}

    ///initializes the iterator from an underlying iterator \p pSub, a predicate \p pPred and an end iterator
    explicit ILines(io::PBufferStream pStream)
        :   mStream(pStream)
        ,   mOk(true)
    {
        Increment();
    }

private:
    ///returns the result of dereferencing the underlying iterator
    typename TFacade::reference Dereference() const
    {
        return mValue;
    }

    ///increments the underlying iterator
    void Increment()
    {
        mOk=bool(*mStream);
        if(mOk)
        {
            getline(*mStream,mValue);
            mOk=bool(*mStream);
        }
    }

    ///returns true if the two underlying iterators contain the same value
    ///
    ///It is OK to use a generic template parameter, since it will only be called for compatible iterators.
    template<typename tO>
    bool Equal(const tO &pO) const
    {
        return mOk==pO.mOk&&(!mOk||mStream==pO.mStream);
    }

    io::PBufferStream mStream;
    std::string mValue;
    bool mOk;
};

///\name Filter
//@{

///returns an \ref IFilter object pointing to the beginning of the range \p pRange
///
///\param pRange the range to which the filtering will be applied
///\param pPred the predicate for filtering. Only elements for which it evaluates to `true` will be included in the range
///
///It will actually point to the first element which matches the given predicate.
inline ILines lines_begin(io::PBufferStream pStream)
{
    return ILines(pStream);
}

///returns an \ref IFilter object pointing past the end of the range \p pRange
///
///\param pRange the range to which the filtering will be applied
///\param pPred the predicate for filtering. Only elements for which it evaluates to `true` will be included in the range
inline ILines lines_end()
{
    return ILines();
}

///returns a range that can be used to access the elements in the range [\p pB,\p pE), filtered by \p pPred
///
///\param pB the beginning of the range to filter
///\param pE the end of the range to filter
inline CRange<ILines> lines_range(io::PBufferStream pStream)
{
    return {lines_begin(pStream),lines_end()};
}

//@}

///@}

/*namespace io*/ } /*namespace buola*/ }

#endif
